/**
 * 时间 ： 2014-6-5 下午5:53:52
 * 作者 ： HaiTao
 * 描述 ： 当前类的描述信息
 **/
package com.laitaoo.component
{
	import mx.controls.AdvancedDataGrid;
	import mx.events.DragEvent;
	import mx.managers.DragManager;
	
	public class CustomAdvancedDataGrid extends AdvancedDataGrid
	{
		public function CustomAdvancedDataGrid()
		{
			super();
		}
		
		override protected function dragOverHandler(event:DragEvent):void{
			super.dragOverHandler(event);
		}
 		override protected function dragDropHandler(event:DragEvent):void{
//			super.dragDropHandler(event);
			
			// Drag-and-drop not supported for cells
			if (isCellSelectionMode())
				return;
			
			if (!(_rootModel is IHierarchicalData) && 
				(event.dragSource.hasFormat("items") || event.dragSource.hasFormat("itemsByIndex")))
			{
				super.dragDropHandler(event);
				return;
			}
			
			if (event.isDefaultPrevented())
				return;
			
			hideDropFeedback(event);
			
			if ((!_rootModel || _rootModel is IHierarchicalData) && event.dragSource.hasFormat("treeDataGridItems"))
			{
				//we only support MOVE by default
				if (event.action == DragManager.MOVE && dragMoveEnabled)
				{
					var items:Array = event.dragSource.dataForFormat("treeDataGridItems") as Array;
					//Are we dropping on ourselves?
					if (event.dragInitiator == this)
					{
						// If we're dropping onto ourselves or a child of a descendant then dont actually drop
						
						calculateDropIndex(event);
						
						// If we did start this drag op then we need to remove first
						var index:int;
						var parent:*;
						var parentItem:*;
						//get ancestors of the drop target item
						var dropParentStack:Array = getParentStack(_dropData.parent);
						dropParentStack.unshift(_dropData.parent); //optimize stack method
						var n:int = items.length;
						for (var i:int = 0; i < n; i++) 
						{ 
							parent = getParentItem(items[i]);
							index = getChildIndexInParent(parent, items[i]);
							//check ancestors of the dropTarget if the item matches, we're invalid
							for each (parentItem in dropParentStack)
							{ 
								//we dont want to drop into one of our own sets of children
								if (items[i] == parentItem)
									return;
							}
							//we remove before we add due to the behavior 
							//of structures with parent pointers like e4x
							removeChildItem(parent, items[i], index);
							//is the removed item before the drop location?
							if (parent == _dropData.parent && index < _dropData.index) 
							{
								addChildItem(_dropData.parent, items[i], (_dropData.index - i - 1));
							}
							else 
							{
								addChildItem(_dropData.parent, items[i], _dropData.index);
							}
						}
					}
				}
			}
			lastDragEvent = null;
		}
		/**
		 *  @private
		 *  Returns the stack of parents from a child item. 
		 */
		private function getParentStack(item:Object):Array
		{
			var stack:Array = [];
			if (item == null)
				return stack;
			
			var parent:* = getParentItem(item);
			while (parent)
			{
				stack.push(parent);
				parent = getParentItem(parent);
			}
			return stack;       
		}
	}
}